package com.example.arffrecorder.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import com.example.arffrecorder.R;
import com.example.arffrecorder.R.drawable;
import com.example.arffrecorder.R.string;
import com.example.arffrecorder.activities.RecordingActivity;

import Utils.ARFFFileWriter;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class ARFFLocationService extends IntentService implements SensorEventListener {

	
	public ARFFLocationService(){
		super("Recording Service");
	}
	
	private NotificationManager myNM;

	private final IBinder mBinder = new LocalBinder();
	
	private final static String DATA_CHANGED = "DATA CHANGED";
	private Intent recordingIntent = new Intent ("com.example.arffrecorder.DATA_CHANGED");

	// the id corresponding to the string that will be displayed by the
	// notification on the start of the service
	private int NOTIFICATION = R.string.record_service_started;

	// sensors needed for recording data from the accelerometer
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	
	private float sensorX, sensorY, sensorZ;
	

	public float getSensorX() {
		return sensorX;
	}

	public void setSensorX(float sensorX) {
		this.sensorX = sensorX;
	}

	public float getSensorY() {
		return sensorY;
	}

	public void setSensorY(float sensorY) {
		this.sensorY = sensorY;
	}

	public float getSensorZ() {
		return sensorZ;
	}

	public void setSensorZ(float sensorZ) {
		this.sensorZ = sensorZ;
	}


	/**
	 * Class for clients to access. Because we know this service always runs in
	 * the same process as its clients, we don't need to deal with IPC.
	 */

	public class LocalBinder extends Binder {
		ARFFLocationService getService() {
			return ARFFLocationService.this;
		}
	}
	
	public void nameFileFromActivity(String newFileName){
		//this.fileName = fileName + ".txt";
		System.out.println("File nameS: " + newFileName);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		// initialize the notification manager for our service
		myNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		// initialize the sensors for getting data from accelerometer
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mAccelerometer = mSensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		// register listener for the accelerometer at the highest rate
		mSensorManager.registerListener(this, mAccelerometer,
				SensorManager.SENSOR_DELAY_FASTEST);

		// Display a notification about starting the service. We put an icon in
		// the status bar.
		showNotification();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		Log.i("LocalService", "Received start id " + startId + ": " + intent);
		// We want this service to continue running until it is explicitly
		// stopped, so return sticky.
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		// Cancel the persistent notification.
		myNM.cancel(NOTIFICATION);

		// Tell the user we stopped.
		Toast.makeText(this, R.string.record_service_stopped,
				Toast.LENGTH_SHORT).show();

		// unregister the sensor from recording data from the accelerometer
		mSensorManager.unregisterListener(this);
		ARFFFileWriter.getInstance().finishWriting();
	}

	@Override
	public IBinder onBind(Intent intent) {
		String fileName = intent.getStringExtra("fileName");
		ARFFFileWriter.getInstance().createFile(fileName);
		System.out.println (fileName);
		return mBinder;
	}
	 
	// display a notification while the service is running
	private void showNotification() {
		// message to be displayed
		CharSequence text = getText(R.string.record_service_started);

		// Set the icon, scrolling text and timestamp
		Notification notification = new Notification(
				R.drawable.notification_icon, text, System.currentTimeMillis());

		// The PendingIntent to launch our activity if the user selects this
		// notification
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, RecordingActivity.class), 0);

		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this,
				getText(R.string.record_service_label), text, contentIntent);

		// Send the notification.
		myNM.notify(NOTIFICATION, notification);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		
		sensorX = (float) (((int) (event.values[0] * 10)) / 10.0);
		sensorY = (float) (((int) (event.values[1] * 10)) / 10.0);
		sensorZ = (float) (((int) (event.values[2] * 10)) / 10.0);
			
		ARFFFileWriter.getInstance().WriteTofile(sensorX, sensorY, sensorZ);
		recordingIntent.putExtra("measurement", sensorX);
		sendBroadcast(recordingIntent); 
	}


	@Override
	protected void onHandleIntent(Intent intent) {
		System.out.println("I'm here intent");
		System.out.println("Intent: " + intent.getStringExtra("name"));
	}

}
